<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\store;

use App\Middleware\User\StoreMiddleware;
use App\Service\Store\BusinessService;
use App\Service\Store\CouponService;
use App\Service\Store\StoreService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 后台商家控制器
 *
 * @AutoController()
 * @Middleware(StoreMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\admin
 */
class BusinessController extends Controller
{

    /**
     * 后台用户服务
     * @Inject
     * @var StoreService
     */
    private $store_service;

    /**
     * @Inject
     * @var BusinessService
     */
    private $business_service;

    /**
     * 优惠券服务
     * @Inject()
     * @var CouponService
     */
    private $coupon_service;

    /**
     * 商家审核搜索
     */
    public function reviews()
    {
        $params = $this->request->all();

        $business_list = $this->business_service->getBizReviewList($params);

        $this->success($business_list);
    }
    

    /**
     * 审核商家列表 - 待审核
     */
    public function BusinessReviewList()
    {
        $result = $this->business_service->BusinessReviewList([
            'status' => 2
        ]);

        $this->success($result);
    }

    /**
     * 审核商家列表 - 已拒绝
     */
    public function BusinessRejectedList()
    {
        $result = $this->business_service->BusinessReviewList([
            'status' => 3
        ]);

        $this->success($result);
    }

    /**
     * 商家审核通过 - 单条
     */
    public function Approved()
    {
        $id = (int)$this->request->input('id', 0);

        $result = $this->business_service->Approved($id);

        !$result ? $this->error()  : $this->success();
    }

    /**
     * 商家审核失败 - 单条
     */
    public function AuditFailed()
    {
        $id = (int)$this->request->input('id', 0);
        $audit_result = (string)$this->request->input('audit_result', '');

        $result = $this->business_service->AuditFailed($id, $audit_result);

        !$result ? $this->error()  : $this->success();
    }

    /**
     * 商家未审核 - 删除
     */
    public function delete()
    {
        $id = (int)$this->request->input('id', 0);

        $result = $this->business_service->delete($id);

        !$result ? $this->error()  : $this->success();
    }

    /**
     * 商家审核通过 - 多条
     */
    public function ManyApproved()
    {
        $id = (array)$this->request->input('id', []);

        $result = $this->business_service->ManyApproved($id);

        !$result ? $this->error()  : $this->success();
    }

    /**
     * 商家审核失败 - 多条
     */
    public function ManyAuditFailed()
    {
        $id = (array)$this->request->input('id', []);
        $audit_result = (string)$this->request->input('audit_result', '');

        $result = $this->business_service->ManyAuditFailed($id, $audit_result);

        !$result ? $this->error()  : $this->success();
    }

    /**
     * 商家未审核 - 删除
     */
    public function ManyDelete()
    {
        $id = (array)$this->request->input('id', 0);

        $business_result = $this->business_service->dao->first([
            'id' => ['in', $id]
        ],['status']);

        foreach ( $business_result as $key => $value ) {
            if( $value['status'] == 1 ) {
                $this->error('BUSINESS.NOT_STATUS');
            }
        }

        $result = $this->business_service->dao->updateByIds([
            'id' => ['in', $id]
        ],[
            'deleted_at' => time()
        ]);

        !$result ? $this->error()  : $this->success();
    }

    /**
     * 商家审核详情
     */
    public function storeDetails()
    {
        $id = (int)$this->request->input('id', 0);

        $result = $this->business_service->storeDetails( $id );

        $this->success($result);
    }

    /**
     * 商家详情内 - 商家审核通过
     */
    public function storeApproved()
    {
        $id = (int)$this->request->input('id', 0);

        $business_result = $this->business_service->dao->first([
            'id' =>$id
        ],['status']);

        if( $business_result->status == 1 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        if( $business_result->status == 3 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        $result = $this->business_service->dao->update($id,[
            'status' => 1
        ]);

        $this->success($result);
    }

    /**
     * 商家详情内 - 商家审核失败
     */
    public function storeAuditFailed()
    {
        $id = (int)$this->request->input('id', 0);
        $audit_result = (string)$this->request->input('audit_result', '');

        $business_result = $this->business_service->dao->first([
            'id' =>$id
        ],['status']);

        if( $business_result->status == 1 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        if( $business_result->status == 3 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        $result = $this->business_service->dao->update($id,[
            'status' => 3,
            'audit_result' => $audit_result,
        ]);

        $this->success($result);
    }

    /**
     * 商家详情内 - 商家商品审核通过
     */
    public function storeCouponApproved()
    {
        $coupon_id = (int)$this->request->input('coupon_id', 0);
        $business_id = (int)$this->request->input('business_id', 0);

        $business_result = $this->business_service->dao->first([
            'id' => $business_id
        ],['status']);

        if( $business_result->status == 2 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        if( $business_result->status == 3 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        $result = $this->business_service->coupon_dao->update(['id'=>$coupon_id],['status' => 1]);

        $this->success($result);
    }

    /**
     * 商家详情内 - 商家商品审核失败
     */
    public function storeCouponAuditFailed()
    {
        $coupon_id = (int)$this->request->input('coupon_id', 0);
        $business_id = (int)$this->request->input('business_id', 0);
        $audit_result = (string)$this->request->input('audit_result', '');

        $business_result = $this->business_service->dao->first([
            'id' => $business_id
        ],['status']);

        if( $business_result->status == 2 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        if( $business_result->status == 3 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        $result = $this->business_service->coupon_dao->update(['id'=>$coupon_id],['status' => 2, 'describe' => $audit_result]);

        $this->success($result);
    }

    /**
     * 商家详情内 - 商家商品审核通过多条
     */
    public function storeCouponManyApproved()
    {
        $coupon_id = (array)$this->request->input('coupon_id', []);
        $business_id = (int)$this->request->input('business_id', 0);

        $business_result = $this->business_service->dao->first([
            'id' => $business_id
        ],['status']);

        if( $business_result->status == 2 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        if( $business_result->status == 3 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        $result = $this->business_service->coupon_dao->update([
            'id'=> ['in', $coupon_id]
        ],[
            'status' => 1
        ]);

        $this->success($result);
    }

    /**
     * 商家详情内 - 商家商品审核失败多条
     */
    public function storeCouponManyAuditFailed()
    {
        $coupon_id = (array)$this->request->input('coupon_id', []);
        $business_id = (int)$this->request->input('business_id', 0);
        $audit_result = (string)$this->request->input('audit_result', "");

        $business_result = $this->business_service->dao->first([
            'id' => $business_id
        ],['status']);

        if( $business_result->status == 2 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        if( $business_result->status == 3 ) {
            $this->error('BUSINESS.NOT_STATUS');
        }

        $result = $this->business_service->coupon_dao->update([
            'id'=> ['in', $coupon_id]
        ],[
            'status' => 2,
            'describe' => $audit_result
        ]);

        $this->success($result);
    }

    /**
     * 商品优惠券审核搜索
     */
    public function coupons()
    {
        $params = $this->request->all();

        $coupon_list = $this->coupon_service->getCouponList($params);

        $this->success($coupon_list);
    }

    /**
     * 优惠券多条通过
     */
    public function couponPass()
    {
        $coupon_ids = (array)$this->request->input('coupon_ids', []);

        $result = $this->coupon_service->dao->update(['id' =>['in', $coupon_ids]], ['status' => 1]);

        !$result && $this->error('COUPON.FAIL_UPDATE_PASS');
        $this->success($result);
    }

    /**
     * 优惠券审核多条拒绝
     */
    public function couponRefuse()
    {
        $coupon_ids = (array)$this->request->input('coupon_ids', []);
        $describe = (string)$this->request->input('describe', "");

        $result = $this->coupon_service->dao->update(['id' =>['in', $coupon_ids]], ['status' => 2, 'describe' => $describe]);

        !$result && $this->error('COUPON.FAIL_UPDATE_REFUSE');

        $this->success($result);
    }

    /**
     * 优惠券审核多条删除
     */
    public function couponDel()
    {
        $coupon_ids = (array)$this->request->input('coupon_ids', []);

        $result = $this->coupon_service->dao->delete(['id' =>['in', $coupon_ids]]);

        !$result && $this->error('COUPON.FAIL_UPDATE_DELETE');

        $this->success($result);
    }

}